// miniprogram/pages/home/home.js
var httpUtils = require('../../utils/httpUtils');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contractTemplate: [],
    showPop: false,
    type: [],
    index: '',
    ossUrl: '',
    path: '',
    filename: '',
    title: '',
    description: '',
    contractForm: {
      title: '',
      description: '',
      type: '',
      ossUrl: '',
      titleRules: [{
          min: 3,
          max: 20,
          message: '标题长度在3-20个字符之间',
          trigger: 'blur'
        },
        {
          required: true,
          message: '请输入标题',
          trigger: 'blur'
        }
      ],
      descriptionRules: [{
          min: 3,
          max: 20,
          message: '描述长度在3-20个字符之间',
          trigger: 'blur'
        },
        {
          required: true,
          message: '请输入描述',
          trigger: 'blur'
        }
      ],
      typeRules: [{
        required: true,
        message: '请选择类型',
        trigger: 'blur'
      }],
      ossUrlRules: [{
        required: true,
        message: '请上传文件',
        trigger: 'blur'
      }]
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // lin-ui表单初始化
    wx.lin.initValidateForm(this);
    wx.request({
        url: 'http://localhost:8080/system/contractTemplate/list',
        method: "GET",
        header: httpUtils.getHeader(),
        success: (res) => {
          console.log(res.data.rows);
          this.setData({
            contractTemplate: res.data.rows
          })
        }
      }),
      wx.request({
        url: 'http://localhost:8080/system/contractType/allList',
        method: "GET",
        header: httpUtils.getHeader(),
        success: (res) => {
          console.log(res.data);
          this.setData({
            type: res.data
          })
        }
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 查看word
   */
  showWord: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.showToast({
      title: '打开中…',
      icon: "loading",
      duration: 100000
    })
    if (url) {
      wx.downloadFile({
        url: url,
        header: {
          'content-type': 'application/json',
        },
        // filePath: wx.env.USER_DATA_PATH + "/res",
        success: function (res) {
          console.log(res)
          var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用 
          wx.openDocument({
            filePath: Path,
            success: function (res) {
              wx.hideToast();
            }
          })
        },
        fail: function (res) {
          console.log(res);
          wx.showToast({
            title: '下载失败',
            icon: "loading",
            duration: 1000
          })
        }
      })
    }
  },

  // 弹窗
  changeShowPop: function () {
    console.log(this.data.showPop);
    this.setData({
      showPop: !this.data.showPop
    })
    // false重置表单
    if (!this.data.showPop) {
      // todo重置表单func
    }
  },

  // 文件上传
  chooseFile: function () {
    var _this = this;
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success(res) {
        console.log(res);
        var size = res.tempFiles[0].size;
        var filename = res.tempFiles[0].name;
        if (filename.indexOf(".pdf") == -1) {
          wx.showToast({
            title: "请上传格式为pdf的文件",
            icon: "none",
            duration: 2000,
            mask: true
          })
        } else {
          console.log(res.tempFiles[0].path);
          wx.showToast({
            title: "上传中，请稍后",
            icon: "none",
            duration: 2000,
            mask: true,
          })
          _this.setData({
              path: res.tempFiles[0].path,
              filename: filename,
            }),
            wx.uploadFile({
              filePath: res.tempFiles[0].path,
              name: 'file',
              header: httpUtils.getHeader(),
              url: 'http://localhost:8080/system/oss/upload',
              success(res) {
                var result = JSON.parse(res.data);
                console.log(res);
                console.log(result);
                wx.showToast({
                    title: '上传成功',
                  }),
                  _this.setData({
                    ossUrl: result.data.url
                  })
              }
            })
        }
      }
    })
  },

  // 选择器
  bindPickerChange: function (e) {
    console.log(e);
    console.log('picker发送选择改变，索引值为', e.detail.value)
    console.log(this.data.type[e.detail.value].type);
    this.setData({
      index: e.detail.value
    })
  },

  //重置表单
  reset() {
    this.setData({
      index: '',
      ossUrl: '',
      path: '',
      filename: '',
      title: '',
      description: '',
    })
  },

  // 提交表单
  submit(event) {
    const {
      detail
    } = event;
    console.log(detail);
    this.submitContractFrom(detail.values);
  },

  submitContractFrom: function (data) {
    var _this = this;
    wx.request({
      url: 'http://localhost:8080/system/contract',
      method: 'POST',
      header: httpUtils.getHeader(),
      data: {
        id: null,
        title: data.contractFormTitle,
        description: data.contractFormDescription,
        type: _this.data.type[_this.data.index].type,
        belong: null,
        ossUrl: _this.data.ossUrl
      },
      success(res) {
        console.log(res);
      },
      fail(err) {
        console.error(err);
      }
    })
  }
})